import Frame from "../frameNamespace";

/**
     * 本地缓存接口
     */
export interface LocalStorage {
    getItem: (key: string) => string,
    setItem: (key: string, value: string | any) => any,
    removeItem: (key: string) => any,
    clear: () => any,
}

export default class LocalStorageService extends Frame.ServiceBase {
    protected _serviceName: string = "本地缓存服务";

    private readonly keyPrefix: string = "UMeGame-";

    constructor() {
        super();

        this._Initialize();
    }

    private localStorage: LocalStorage | any = undefined;

    _Initialize(): void {
        super._Initialize();

        this.localStorage = cc.sys.localStorage;
    }

    set(key: string, value: string | any) {
        //XXX 设置本地缓存
        if (this.localStorage === undefined) {
            console.error("本地缓存服务未正常工作 --- localStorage属性未赋值");
            return;
        }
        this.localStorage.setItem(this.keyPrefix + key, value);
    }

    get(key: string): string {
        //XXX 获取本地缓存
        if (this.localStorage === undefined) {
            console.error("本地缓存服务未正常工作 --- localStorage属性未赋值");
            return null;
        }

        return this.localStorage.getItem(this.keyPrefix + key);
    }
}